//
//  YHViewController1.m
//  LyhToolsTest
//
//  Created by LYH on 2018/10/26.
//  Copyright © 2018年 LYH.com. All rights reserved.
//

#import "YHcollectionVC.h"
#import "YHImageCell.h"
#import "YHMymenuItemCell.h"
#import "YHcollectHeadview.h"
@interface YHcollectionVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) TSEmptyView *emptyview;
@property (nonatomic, assign) NSInteger itemscount;
@end

@implementation YHcollectionVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initnava];
     [self refresh];
     _itemscount = 10;
    [self Getdatawork];
}
- (void)initnava
{
    WeakSelf;
    wself.ts_navgationBar = [TSNavigationBar navWithTitle:@"collection实x验" backAction:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
-(void)refresh{
    WeakSelf;
    self.collectionview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        wself.itemscount =0;
        [wself Getdatawork];
    }];
    self.collectionview.mj_footer = [ MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        wself.itemscount ++;
        [wself Getdatawork];
    }];
    
}

#pragma -mark ---------------------collectionView的代理和数据源方法---------------------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _itemscount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        YHImageCell *cell = [YHImageCell codeCell:collectionView cellForItemAtIndexPath:indexPath];
        cell.imageViewitem.image = [UIImage imageNamed:@"image1"];
//        NSString * url = [NSString stringWithFormat:@""];
//
//        [cell.imageViewitem sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];

        return cell;
    } else {
        YHMymenuItemCell *cell = [YHMymenuItemCell xibCell:collectionView cellForItemAtIndexPath:indexPath];
        return cell;
    }
   
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
//cell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         return  CGSizeMake((kScreenWidth-60)/4,(kScreenWidth-60)/4);
    } else {
         return  CGSizeMake((kScreenWidth-3)/4,(kScreenWidth-3)/4);
    }
}
//section 边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(15, 15, 0, 15);
    } else {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
}
//
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section ==0) {
        return 5;
    } else {
        return 1;
    }
 }
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section ==0) {
        return 10;
    } else {
        return 1;
    }
}

//头尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        YHcollectHeadview *header = [YHcollectHeadview codeHeaderView:collectionView cellForItemAtIndexPath:indexPath];
        header.Titlelab.text = @"头部哦";
        return header;
    } else {
        YHcollectHeadview *Footer = [YHcollectHeadview codeFooterView:collectionView cellForItemAtIndexPath:indexPath];
        Footer.Titlelab.text = @"尾部哦";
        return Footer;
    }
   
}
- (void)Getdatawork
{
    [self.collectionview.mj_header endRefreshing];
    [self.collectionview.mj_footer endRefreshing];
    if ( _itemscount == 0) {
        self.collectionview.hidden = YES;
        self.emptyview.hidden = NO;
    } else {
        self.collectionview.hidden = NO;
        self.emptyview.hidden = YES;
    }
    [self.collectionview reloadData];
}
#pragma -mark ————————————————————Lay————————————————————
//懒加载collectionview
- (UICollectionView *)collectionview
{
    if (!_collectionview) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection=UICollectionViewScrollDirectionVertical;//竖直滚动
        layout.minimumInteritemSpacing=5;
        layout.minimumLineSpacing=10;
        //头部
        layout.headerReferenceSize = CGSizeMake(kScreenWidth, 40);
        //尾部
        layout.footerReferenceSize = CGSizeMake(kScreenWidth, 40);
        _collectionview=[[UICollectionView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeigth-SafeAreaTopHeight-SafeAreaBoHttomHeight) collectionViewLayout:layout];
        _collectionview.dataSource=self;
        _collectionview.delegate=self;
        _collectionview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        //支持分页
        _collectionview.pagingEnabled=NO;
        _collectionview.scrollEnabled=YES;
        [self.view addSubview:_collectionview];
    }
    return _collectionview;
}
- (TSEmptyView *)emptyview
{
    if (!_emptyview) {
        _emptyview = [[TSEmptyView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeigth-SafeAreaTopHeight) SetTipsTitle:@"没数据哦～～" SetBackImageName:@"without_evaluate" SetBtntitle:@"我要打天下"  isShow:NO BtnAction:^{
             self.itemscount = 5;
            [self Getdatawork];
        }];
        _emptyview.clickBtn.backgroundColor = ThemeColor;
        [self.view addSubview:_emptyview];
    }
    return _emptyview;
}
@end
